%% humans
% listMeasName = {
%     '191108_Bob_HumanBlood1'
%     '191113_Bob_HumanBlood2'
%     '201118_DoreenBob_HumanBlood3'
%     '201125_DoreenBob_HumanBlood4'
%     '201126_DoreenBob_HumanBlood5'
%     };
% targetName = 'human';

%% bats 1. round
% listMeasName = {
%     '191015_GeraldDoreenBob_Bats1'
%     '191016_GeraldDoreenBob_Bats2'
%     '191030_GeraldDoreenBob_Bats3'
%     '201008_GeraldDoreenBob_Bats4'
%     '201021_GeraldDoreenBob_Bats5'
%     '201028_GeraldDoreenBob_Bats6'
%     };
% targetName = 'bat1';

%% bats 2. round
% listMeasName = {
%     '210408_FaruqBob_Bats21'
%     '210518_Faruq_1Wildbat'
%     '210520_Faruq_1Wildbat'
%     '210527_Faruq_1Wildbat'
%     '210809_Faruq_2Wildbats'
%     '210811_Faruq_2_Wildbats'
%     '210819_Faruq_2Wildbats'
%     '210825_Faruq_1Wildbat'
%     '210916_Faruq_1Wildbat'
%     '210923_Faruq_2Wildbats'
%     '210929_Faruq_2Wildbats'
%     '211012_Faruq_2wildbats'
%     '211019_Faruq_2Wildbats'
%     '211026_Faruq_2Wildbats'
%     '211029_Faruq_2wildbats'
%     };
% targetName = 'bat2';

%% FLI bats
listMeasName = {
    '210812_Faruq_2_FLIbats'
    '210902_Faruq_2FLIbats'
    '210917_Faruq_2FLIBats'
    '210924_Faruq_2FLIBats'
    '211001_Faruq_2FLIbats'
    '211007_Faruq_2FLIBats'
    };
targetName = 'batFLI';

%% para
flagGate = 0;
spaceY = 20;

%% script: combine result plots
listImages = {'Area','Velocity','Tau1','Tau2','DeltaDefoTau1','DeltaDefoTau2'};
resultKey = '_result';

if flagGate
    strGate = '_gate';
else
    strGate = '_noGate';
end

for idxListImages = 1:length(listImages)
    fprintf('%s\n', listImages{idxListImages});
    %% read image sizes
    sizeImages = zeros(length(listMeasName),2);
    for idxListMeasName = 1:length(listMeasName)
        measDir = ['..' filesep listMeasName{idxListMeasName} filesep];

        pngFile = dir([measDir '*' resultKey strGate '_' listImages{idxListImages} '.png']);
        img = imread([pngFile.folder filesep pngFile.name]);
        img = autoCrop(img);
        sizeImages(idxListMeasName,:) = [size(img,1), size(img,2)];
    end
    %% concatenate images
    sizeImage = [max(sizeImages(:,1)), max(sizeImages(:,2))];
    imgCat = repmat(uint8(255),sum(sizeImages(:,1))+(length(listMeasName)-1)*spaceY,sizeImage(2),3);
    
    yPos = 1;
    for idxListMeasName = 1:length(listMeasName)
        measDir = ['..' filesep listMeasName{idxListMeasName} filesep];

        pngFile = dir([measDir '*' resultKey strGate '_' listImages{idxListImages} '.png']);
        img = imread([pngFile.folder filesep pngFile.name]);
        img = autoCrop(img);
        img = imgExtend(img, [size(img,1) sizeImage(2)], 'c');
        imgCat(yPos:yPos + size(img,1)-1,...
            1:sizeImage(2), :) = img;
        yPos = yPos + size(img,1) + spaceY;
    end
    imwrite(imgCat, [targetName '_result' strGate '_' listImages{idxListImages} '.png']);
end

return
%% funcrions
function img = autoCrop(img)
mask = (img == 255);
% mask = all(mask,4);
mask = all(mask,3);
maskX = all(mask,1);
maskY = all(mask,2);
idxMaskXZero = find(maskX == false);
idxMaskYZero = find(maskY == false);
img = img(idxMaskYZero(1):idxMaskYZero(end),idxMaskXZero(1):idxMaskXZero(end),:,:);

end

function imgExt = imgExtend(img, targetSize, mode)
if mode == 'c'
    shiftY = round((targetSize(1) - size(img,1))/2);
    shiftX = round((targetSize(2) - size(img,2))/2);
    imgExt = repmat(uint8(255),targetSize(1),targetSize(2),3);
    imgExt(1+shiftY:1+shiftY+size(img,1)-1, 1+shiftX:1+shiftX+size(img,2)-1, :) = img;
end
end